﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYConnectBatch : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				IList<Duct> list = new List<Duct>();
				ISelectionFilter selectionFilter = new DuctSelectionFilter();
				HYDuctConnect hyductConnect = new HYDuctConnect();
				IList<Element> list2 = new List<Element>();
				string text = "批量连接";
				int num = 0;
				transaction = new Transaction(document, text);
				transaction.Start();
				for (;;)
				{
					SubTransaction subTransaction = new SubTransaction(document);
					try
					{
						if (num != 0)
						{
							RevitVersionFuncs.ClearSelection(selection);
							list2.Clear();
							list.Clear();
						}
						num++;
						if (selection.GetElementIds().Any<ElementId>())
						{
							IEnumerator<ElementId> enumerator = selection.GetElementIds().GetEnumerator();
							{
								while (enumerator.MoveNext())
								{
									ElementId elementId = enumerator.Current;
									list2.Add(document.GetElementById(elementId));
								}
								goto IL_F6;
							}
						}
						list2 = selection.PickElementsByRectangle(selectionFilter, "请框选所要连接的风管");
						IL_F6:
						foreach (Element element in list2)
						{
							if (element is Duct)
							{
								list.Add(element as Duct);
							}
						}
						subTransaction.Start();
						if (list != null && list.Count >= 2)
						{
							if (!hyductConnect.NewBeatchFitting(list))
							{
								MessageBox.Show("连接失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							}
							else
							{
								document.Regenerate();
							}
						}
						subTransaction.Commit();
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
						break;
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					}
					finally
					{
						if (subTransaction != null)
						{
							TransactionStatus status = subTransaction.GetStatus();
							if ((int)status == 1)
							{
								subTransaction.Commit();
							}
							else if ((int)status == 5)
							{
								subTransaction.RollBack();
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status2 = transaction.GetStatus();
					if ((int)status2 == 1)
					{
						transaction.Commit();
					}
					else if ((int)status2 == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
